import Vue from "vue";
import Vuex from "vuex";
import {UserService} from "@/services/user.service";
// @ts-ignore
import toast, { TOAST_TYPES } from "@/store/modules/toast.store";
import { Cache } from "@/common/cache";
import router, { HOME_ROUTE } from "@/router";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userId: "",
    name: "",
    client_tag: 0
  },
  getters: {
    userId: (state: any) => {
      return state.userId;
    },
    name: (state: any) => {
      return state.name;
    },
    clientTag: (state: any) => {
      return state.client_tag;
    },
    isLoggedIn: (state: any) => {
      return state.userId !== "";
    },
    isStudent: (state: any) => {
      return state.userId !== "" && state.client_tag == 0;
    },
    isTeacher: (state: any) => {
      return state.userId !== "" && state.client_tag == 1;
    }
  },
  mutations: {
    setUserId(state: any, userId: string): void {
      state.userId = userId;
    },
    setName(state: any, name: string): void {
      state.name = name;
    },
    setClientTag(state: any, client_tag: number): void {
      state.client_tag = client_tag;
    },
    removeUser(state: any): void {
      state.userId = "";
      state.name = "";
      state.client_tag = 0;
    }
  },
  actions: {
    login({ commit, dispatch }: any, { name, password }: any): Promise<void> {
      return new Promise((resolve: any, reject: any) => {
        UserService.login(name, password).then((res: any) => {
          if(res.result) {
            commit("setUserId", res.userId);
            commit("setClientTag", res.client_tag);
            commit("setName", res.name);
            Cache.setUserId(res.userId);
            Cache.setClientTag(res.client_tag);
            Cache.setUserName(res.name);
            dispatch("toast/makeToast", { message: "登录成功", type: TOAST_TYPES.Success });
            resolve();
          } else {
            dispatch("toast/makeToast", { message: res.message, type: TOAST_TYPES.Error });
            reject();
          }
        }).catch((err: any) => {
          reject(err);
        });
      });
    },
    register({commit, dispatch }: any, { name, password, client_tag, t_password}: any):Promise<void> {
      return new Promise((resolve, reject) => {
        UserService.register(name, password, client_tag, t_password).then((res: any) => {
          if(res.result) {
            commit("setUserId", res.userId);
            commit("setClientTag", res.client_tag);
            commit("setName", res.name);
            Cache.setUserId(res.userId);
            Cache.setClientTag(res.client_tag);
            Cache.setUserName(res.name);
            dispatch("toast/makeToast", { message: "注册成功", type: TOAST_TYPES.Success });
            resolve();
          } else {
            dispatch("toast/makeToast", { message: res.message, type: TOAST_TYPES.Error });
            reject();
          }
        }).catch((err: any) => {
          reject(err);
        });
      });
    },
    logout({ commit, dispatch }: any): void {
      UserService.logout().then((res: any) => {
        if (res.result) {
          dispatch("toast/makeToast", { message: "登出成功", type: TOAST_TYPES.Success });
          commit("removeUser");
          Cache.removeUserId();
          Cache.removeClientTag();
          Cache.removeUserName();
          router.push({ name: HOME_ROUTE.name }).then().catch();
        } else {
          dispatch("toast/makeToast", { message: res.message, type: TOAST_TYPES.Error });
        }
      });
    }
  },
  modules: {
    toast
  },
});
